package com.meitianhui.convenitenbuy.module.home.ui.activity.web;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;

import com.meitianhui.convenitenbuy.Constants;
import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.base.BaseWebViewActivity;
import com.meitianhui.convenitenbuy.config.InfoProvider;
import com.meitianhui.convenitenbuy.main.model.bean.EventBean;
import com.meitianhui.convenitenbuy.util.RxBus;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by Administrator on 2017/11/8.
 */

public class AdWebViewActivity extends BaseWebViewActivity {

    // 页面展示的内容的类型。
    private String type = null;

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        if(!TextUtils.isEmpty(mTitle)) {
            setTbTitle(mTitle);
        }
        setTbMenu(R.mipmap.icon_title_close);
        this.type = getIntent().getStringExtra("type");
        mWebView.addJavascriptInterface(new MyJavaScriptInterface(this), "jsObj");//js调用安卓
        mWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
    }

    @Override
    protected void onRefreshData() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //通知去关闭广告界面
        RxBus.get().post(new EventBean(Constants.EVENT_TYPE_ADV_CLOSE_REDPACKET_SHOW));
    }

    //js调用安卓代码
    private class MyJavaScriptInterface {
        private Handler handler = new Handler();
        private Context mContext;

        public MyJavaScriptInterface(Context c) {
            this.mContext = c;
        }


        //返回按钮
        @JavascriptInterface
        public void closeWebView() {
            AdWebViewActivity.this.finish();
        }

        @JavascriptInterface
        public void  mobileFeeRecharge(final String s_1){
            if(!TextUtils.isEmpty(s_1)){
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
//                        Intent intent = new Intent(AdWebViewActivity.this, PayPhoneRechargActivity.class);
//                        intent.putExtra("bean", s_1);
//                        startActivity(intent);
                    }
                }, 0);
            }
        }

        // 跳到领了么详情
        // Add a new parameter "dataSource", jump to different pattern of web page
        // according to data source.
        @JavascriptInterface
        public void getGoods_Id(final String params) {
            String goodsId = null;
            String dataSource = null;
            try {
                JSONObject jsonObject = new JSONObject(params);
                goodsId = jsonObject.optString("goods_id");
                dataSource = jsonObject.optString("data_source");
            } catch (JSONException e) {
                e.printStackTrace();
                // 这个一个共用的页面，getGoods_Id 是共用的回调。
                // 新人专享返回json字符串，其它返回goodsId.
                goodsId = params;
                dataSource = "";
            }

            if (TextUtils.isEmpty(goodsId)) return;

            final String finalGoodsId = goodsId;
            final String finalDataSource = dataSource;
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    Intent intent = new Intent(AdWebViewActivity.this, EdinburghWebViewActivity.class);
                    if (InfoProvider.getLoginStatus(AdWebViewActivity.this)) {
                        String pattern;
                        String title;
                        switch (finalDataSource) {
                            case "meitianhui":
                                pattern = "h5/c/payback/ziying-detail?goods_id=";
                                title = "自营";
                                break;
                            case "huiguo":
                                pattern = "h5/c/payback/huiguo-detail?goods_id=";
                                title = "会过";
                                break;
                            default:
                                pattern = "h5/c/payback/detail?goods_id=";
                                title = "淘淘领";
                                break;
                        }

                        intent.putExtra("url", Constants.HOST + pattern + finalGoodsId
                                + "&mi=" + InfoProvider.getUserToken(AdWebViewActivity.this).getMember_id()
                                + "&mobile=" + InfoProvider.getUserToken(AdWebViewActivity.this).getMobile()
                                + "&bind=1");
                        intent.putExtra("title", title);
                        intent.putExtra("is_second", true);

                    } else {
                        intent.putExtra("url", Constants.HOST
                                + "h5/c/payback/detail?goods_id=" + finalGoodsId + "&bind=1");
                        intent.putExtra("title", "淘淘领");
                        intent.putExtra("is_second", true);
                    }

                    startActivity(intent);
                }
            }, 0);
        }
    }

    @Override
    protected void isFinish() {
        if ("limit".equals(type)) {
            // 商品展示的是“限时抢购”的内容，不用返回上一个页面。直接 finish.
            finish();
            return;
        }
        if (mWebView.canGoBack()&& !"about:blank".equals(mWebView.getUrl())) {
            mWebView.goBack();// 返回前一个页面
        } else {
            finish();
        }
    }

    @Override
    protected void shareWxMini(View v) {

    }

    @Override
    protected void clickMenu() {
        super.clickMenu();
        finish();
    }

    @Override
    protected void urlLoading(WebView view, String url) {
        view.loadUrl(url);
    }

    @Override
    protected void receivedTitle(WebView view, String title) {
        if ("about:blank".equals(title)|| TextUtils.isEmpty(title)){
            if (!TextUtils.isEmpty(mTitle)){
                setTbTitle(mTitle);
            }
        }else {
            setTbTitle(title);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (mWebView.canGoBack()&& !"about:blank".equals(mWebView.getUrl())) {
                mWebView.goBack();
            } else {
                finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}
